package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.requestlog.RequestLog;
import com.whq.thrift.api.web.admin.service.requestlog.RequestLogService;
import com.whq.thrift.api.web.admin.util.DateFormatUtil;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class RequestLogController {
	
	private static final int DEFAULT_PAGE_SIZE = 25;
	
	@Autowired
	RequestLogService requestLogService;
	
	@RequestMapping(value="bs/requestlog/list")
	public String list(
	        @RequestParam(value="time", required=false) String time,
	        @RequestParam(value="start_time", required=false) String start_time,
	        @RequestParam(value="end_time", required=false) String end_time,
	        @RequestParam(value="method", required=false) String method,
	        @RequestParam(value="phone", required=false) String phone,
	        @RequestParam(value="flow_id", required=false) String flow_id,
	        @RequestParam(value="app_version", required=false) String app_version,
	        @RequestParam(value="os_type", required=false) String os_type,
	        @RequestParam(value="verify_code", required=false) Integer verify_code,
	        @RequestParam(value="result_code", required=false) String result_code,
	        @RequestParam(value="minCost", required=false) Long minCost,
	        @RequestParam(value="maxCost", required=false) Long maxCost,
	        @RequestParam(value="currentPage", required=true, defaultValue="1") Integer currentPage,
	        @RequestParam(value="pageSize", required=false) Integer pageSize,
			Model model) {
		RequestLog requestLog = new RequestLog();
		PageData<RequestLog> pageData = new PageData<RequestLog>();
	    pageData.setCurrentPage(currentPage);
	    if ( (pageSize == null) || (pageSize <= 0) ){
	    	pageSize = DEFAULT_PAGE_SIZE;
	    }
	    pageData.setPageSize(pageSize);
		
	    // 查询参数
	    Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    
	    if (time != null && StringUtil.isNotBlank(time)) {
	    	whereParams.put("time", DateFormatUtil.dateToString(DateFormatUtil.formatToDate(time)));
	    	requestLog.setTime(DateFormatUtil.formatToDate(time));
	    }
	    
	    int tableSeq = requestLog.getTableSeq();
	    whereParams.put("tableSeq", tableSeq);
	    
	    if (StringUtil.isNotBlank(start_time)) {
	    	whereParams.put("start_time", start_time);
	    } else {
	    	whereParams.put("start_time", "00:00:00");
	    }
	    
	    if (StringUtil.isNotBlank(end_time)) {
	    	whereParams.put("end_time", end_time);
	    } else {
	    	whereParams.put("end_time", "23:59:59");
	    }
	    
	    if (StringUtil.isNotBlank(method)) {
	    	whereParams.put("method", method);
	    }
	    
	    if (StringUtil.isNotBlank(phone)) {
	    	whereParams.put("phone", phone);
	    }
	    
	    if (StringUtil.isNotBlank(flow_id)) {
	    	whereParams.put("flow_id", flow_id);
	    }
	    
	    if (StringUtil.isNotBlank(app_version)) {
	    	whereParams.put("app_version", app_version);
	    }
	    
	    if (StringUtil.isNotBlank(os_type)) {
	    	whereParams.put("os_type", os_type);
	    }
	    
	    if (verify_code != null) {
	    	whereParams.put("verify_code", verify_code);
	    }
	    
	    if (StringUtil.isNotBlank(result_code)) {
	    	whereParams.put("result_code", result_code);
	    }
	    
	    if (minCost != null) {
	    	whereParams.put("minCost", minCost);
	    }
	    
	    if (maxCost != null) {
	    	whereParams.put("maxCost", maxCost);
	    }
	    
	    int totalCount = requestLogService.findRecordCount(whereParams);
	    pageData.setTotalCount(totalCount);
	    
	    int beginIndex = (currentPage - 1) * pageSize;
	    List<RequestLog> resultList = requestLogService.findRecordsByMap(whereParams, beginIndex, pageSize);
	    pageData.setResultList(resultList);
	    
	    model.addAttribute("time", time);
	    model.addAttribute("start_time", start_time);
	    model.addAttribute("end_time", end_time);
	    model.addAttribute("method", method);
	    model.addAttribute("phone", phone);
	    model.addAttribute("flow_id", flow_id);
	    model.addAttribute("app_version", app_version);
	    model.addAttribute("os_type", os_type);
	    
	    model.addAttribute("verify_code", verify_code);
	    model.addAttribute("result_code", result_code);
	    model.addAttribute("minCost", minCost);
	    model.addAttribute("maxCost", maxCost);
	    
		model.addAttribute("pageData", pageData);
		return "bs/requestlog/list";
	}
	
	@RequestMapping(value="bs/requestlog/show")
	public String show(@RequestParam(value="id", required=true) String id,
					   @RequestParam(value="time", required=true) String time,
			Model model){ 
		if (id == null) {

			return "error";
		}
		RequestLog record = this.requestLogService.findRecordById(id, time);
        model.addAttribute("record", record);
        if (record == null) {

			return "error";
		}
        return "bs/requestlog/show";  
    }

}
